package com.zh.rent.controller.backstage;

import com.github.pagehelper.PageInfo;
import com.zh.rent.pojo.Account;
import com.zh.rent.pojo.Guide;
import com.zh.rent.service.AccountService;
import com.zh.rent.service.IGuideService;
import com.zh.rent.util.*;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.IOException;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.*;

@Controller
@RequestMapping("/bacGuide")
@Api(tags="新手引导反馈客服后台管理")
public class BacGuideController {
    @Autowired
    IGuideService IGuideServiceImpl;
    @Autowired
    AccountService AccountServiceImpl;

    @PostMapping("/BacGuidesel")
    @ResponseBody
    @ApiOperation(value = "显示")
    public JsonResult BacGuidesel(@RequestParam(value="pageSize",defaultValue="5")Integer pageSize,
                                  @RequestParam(value="pageNum",defaultValue="1")Integer pageNum,
                                  Integer pathType,String name) {
        PageInfo<Guide> mapPageInfo=null;
        Map<String,Object> map = new HashMap<>();
        name = "%"+name+"%";
        map.put("name",name);
        map.put("pathType",pathType);
        if(pathType!=1){
            mapPageInfo = IGuideServiceImpl.selguiUrl(pageNum, pageSize, map);
            for (Guide guide : mapPageInfo.getList()) {
                guide.setRemark(guide.getRemark()==null? "":guide.getRemark());
            }
            return JsonResult.OK(mapPageInfo);
        }else {
            PageInfo<Map<String, Object>> mapPageInfo1 = IGuideServiceImpl.selguiFee(pageNum, pageSize, map);
            for (Map<String, Object> stringObjectMap : mapPageInfo1.getList()) {
               /* Integer accountid = (Integer)stringObjectMap.get("Accountid");
                Account account = AccountServiceImpl.selAcc(accountid);
                stringObjectMap.put("AccountName",account.getAccountName());
                stringObjectMap.put("MailboxName",account.getMailboxName());*/
                stringObjectMap.put("Remark",stringObjectMap.get("Remark")==null?"":stringObjectMap.get("Remark"));

            }
            return JsonResult.OK(mapPageInfo1);
        }
    }


    /*@PostMapping("/upLoadAccessory")
    @ResponseBody
    @ApiOperation(value = "图片上传服务器")
    public JsonResult upLoadAccessory(@RequestParam("file")MultipartFile file){
        Map<String,Object> map = new HashMap<>();
        String url=null;
        if (file.isEmpty()) {
            return JsonResult.ERROR("上传为空");
        }else {
            //保存时的文件名
            DateFormat df = new SimpleDateFormat("yyyyMMddHHmmss");
            Calendar calendar = Calendar.getInstance();
            String dateName = df.format(calendar.getTime())+file.getOriginalFilename();

            System.out.println(dateName);
            //保存文件的绝对路径
            WebApplicationContext webApplicationContext = (WebApplicationContext) SpringContextUtils.applicationContext;
            ServletContext servletContext = webApplicationContext.getServletContext();
            String realPath = servletContext.getRealPath("/");
            String filePath = realPath + "img"+File.separator+dateName;
            System.out.println("绝对路径:"+filePath);
            File newFile = new File(filePath);
            //MultipartFile的方法直接写文件
            try {
                //上传文件
                file.transferTo(newFile);
                //数据库存储的相对路径
                String projectPath = servletContext.getContextPath();
                HttpServletRequest request = HttpContextUtils.getHttpServletRequest();
                String contextpath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+projectPath;
                url ="/img/"+dateName;
                System.out.println("相对路径:"+url);
                //文件名与文件URL存入数据库表
                map.put("url",url);
                return JsonResult.OK(map);
            } catch (IllegalStateException | IOException e) {
               return JsonResult.ERROR("上传失败");
            }
        }
    }*/

    @RequestMapping("/upLoadAccessory")
    @ResponseBody
    @ApiOperation(value = "图片上传服务器")
    public JsonResult upload(HttpServletRequest requet, @ModelAttribute MultipartFile file ){
        try {
            if (!file.isEmpty()) {
                System.out.println("2222" + requet.getScheme());
                System.out.println("3333" + requet.getServerName());
                System.out.println("44444" + requet.getServerPort());
                System.out.println("55555" + FileHandleUtil.upload(file.getInputStream(), "file/", file.getOriginalFilename()));
                String result = requet.getScheme() + "://" + requet.getServerName() + ":" + requet.getServerPort() + FileHandleUtil.upload(file.getInputStream(), "file/", file.getOriginalFilename());
               String url=FileHandleUtil.upload(file.getInputStream(), "file/", file.getOriginalFilename());
               Map<String,Object> map = new HashMap<>();
               map.put("url",url);
                return JsonResult.OK(map);
            } else {
                return JsonResult.ERROR("上传失败");
            }
        }catch (Exception e){
            return JsonResult.ERROR("上传出错："+e.getMessage());
        }

    }

    @PostMapping("/BacGuideupdate")
    @ResponseBody
    @ApiOperation(value = "修改")
    public JsonResult BacGuideupdate(Integer guideid,String guidetext,String url,String invCode,String guidePath,String Remark) {
        Guide guide = new Guide();
        if(guidetext==null && guidetext==""){
            guidetext="";
        }
        if(url==null && url==""){
            url="";
        }
        if(invCode==null && invCode==""){
            invCode="";
        }
        if(guidePath==null && guidePath==""){
            guidePath="";
        }
        if(Remark==null && Remark==""){
            Remark="";
        }
        guide.setUrl(url);
        guide.setInvCode(invCode);
        guide.setGuidetext(guidetext);
        guide.setGuideid(guideid);
        guide.setGuidePath(guidePath);
        guide.setRemark(Remark);
        int i = IGuideServiceImpl.updateGuide(guide);
        if(i==0){
            return JsonResult.ERROR("修改失败");
        }
        return JsonResult.OK("修改成功");
    }

    @PostMapping("/upguidehand")
    @ResponseBody
    @ApiOperation(value = "新手引导上传")
    public JsonResult upguidehand(String guidetext,String guidePath,String Remark){
                //文件名与文件URL存入数据库表
                Guide guide = new Guide();
                guide.setGuidePath(guidePath);//路径
                guide.setGuidetext(guidetext);//文字
                guide.setPathType(0);//类型
                guide.setInvCode("");//邀请码
                guide.setUrl("");//超链接
                guide.setRemark(Remark==null?"":Remark);
                int i = IGuideServiceImpl.insetGuide(guide);
                if(i==0){
                    return JsonResult.ERROR("保存失败");
                }
        return JsonResult.OK("上传成功");
    }

    @PostMapping("/upguideservice")
    @ResponseBody
    @ApiOperation(value = "客服上传")
    public JsonResult upguideservice(String guidetext,String url,String guidePath,String Remark){

            //文件名与文件URL存入数据库表
            Guide guide = new Guide();
            guide.setGuidePath(guidePath);//路径
            guide.setGuidetext(guidetext);//文字
            guide.setPathType(2);//类型
            guide.setInvCode("");//邀请码
            guide.setUrl(url);//超链接
        guide.setRemark(Remark==null?"":Remark);
            int i = IGuideServiceImpl.insetGuide(guide);
            if(i==0){
                return JsonResult.ERROR("保存失败");
            }
        return JsonResult.OK("上传成功");
    }


    @PostMapping("/delguid")
    @ResponseBody
    @ApiOperation(value = "单个删除")
    public JsonResult delguid(Integer guideid){
        try {
            Integer i = IGuideServiceImpl.delguide(guideid);
            return JsonResult.OK("删除成功");
        }catch (Exception e){
            return JsonResult.ERROR("删除失败");
        }
    }

    @PostMapping("/delguidAll")
    @ResponseBody
    @ApiOperation(value = "id批量删除")
    public JsonResult delguidAll(String guideid) {
        try {
            String buy= guideid.substring(1,guideid.length()-1);
            List<Integer> list = new ArrayList<Integer>();
            String[] str = buy.split(",");
            for (int i = 0; i < str.length; i++) {
                int i1 = Integer.parseInt(str[i]);
                list.add(i1);
            }
            Integer i = IGuideServiceImpl.delguideAll(list);
            return JsonResult.OK("删除成功");
        }catch (Exception e){
            return JsonResult.ERROR("删除失败");
        }
    }
}
